#include <stdio.h>

main(unsigned argc, const char *argv[])
{
	if (argc != 2) {
		fprintf(stderr, "Usage: %s INPUT_FILE\n", argv[0]);
		fprintf(stderr, "      INPUT_FILE: For example, velocity.0030.txt\n");
		return -1;
	}

	// Figure out what input file to open and open it.
	const char *input_file_name = argv[1];
	FILE *infile;
	if ( (infile = fopen(input_file_name, "r")) == NULL) {
		fprintf(stderr,"Could not open %s for reading\n", input_file_name);
		return -2;
	}

	// Figure out what output file to open and open it.
	char	output_file_name[4096];
	sprintf(output_file_name, "%s.bin", input_file_name);
	FILE *outfile;
	if ( (outfile = fopen(output_file_name, "wb")) == NULL) {
		fprintf(stderr,"Could not open %s for reading\n", output_file_name);
		return -3;
	}

	// Scan each input line and write it to the output file
	unsigned long l;
	float	vel[3];
	for (l = 0; l < 36902400; l++) {
		if (fscanf(infile, "%f %f %f", &vel[0], &vel[1], &vel[2]) != 3) {
			fprintf(stderr,"Could not read line %ld\n", l);
			return -4;
		}
		if (fwrite(vel, sizeof(vel[0]), 3, outfile) != 3) {
			fprintf(stderr,"Could not write line %ld\n", l);
			return -5;
		}
	}

	fclose(infile);
	fclose(outfile);

	return 0;
}

